package com.ruoyi.ruoyiyinzheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ruoyiyinzheng.domain.bo.StExperimentDatasViewBo;
import com.ruoyi.ruoyiyinzheng.domain.vo.StExperimentDatasViewVo;
import com.ruoyi.ruoyiyinzheng.domain.StExperimentDatasView;
import com.ruoyi.ruoyiyinzheng.mapper.StExperimentDatasViewMapper;
import com.ruoyi.ruoyiyinzheng.service.IStExperimentDatasViewService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 全部实验数据Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-16
 */
@RequiredArgsConstructor
@Service
public class StExperimentDatasViewServiceImpl implements IStExperimentDatasViewService {

    private final StExperimentDatasViewMapper baseMapper;

    /**
     * 查询全部实验数据
     */
    @Override
    public StExperimentDatasViewVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询全部实验数据列表
     */
    @Override
    public TableDataInfo<StExperimentDatasViewVo> queryPageList(StExperimentDatasViewBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StExperimentDatasView> lqw = buildQueryWrapper(bo);
        Page<StExperimentDatasViewVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询全部实验数据列表
     */
    @Override
    public List<StExperimentDatasViewVo> queryList(StExperimentDatasViewBo bo) {
        LambdaQueryWrapper<StExperimentDatasView> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StExperimentDatasView> buildQueryWrapper(StExperimentDatasViewBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StExperimentDatasView> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getExperimentNumber()), StExperimentDatasView::getExperimentNumber, bo.getExperimentNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getClorlNumber()), StExperimentDatasView::getClorlNumber, bo.getClorlNumber());
        lqw.like(StringUtils.isNotBlank(bo.getColorSchemeName()), StExperimentDatasView::getColorSchemeName, bo.getColorSchemeName());
        lqw.like(StringUtils.isNotBlank(bo.getStRecieptName()), StExperimentDatasView::getStRecieptName, bo.getStRecieptName());
        lqw.eq(StringUtils.isNotBlank(bo.getCo2()), StExperimentDatasView::getCo2, bo.getCo2());
        lqw.eq(StringUtils.isNotBlank(bo.getTemperature()), StExperimentDatasView::getTemperature, bo.getTemperature());
        lqw.eq(StringUtils.isNotBlank(bo.getPressure()), StExperimentDatasView::getPressure, bo.getPressure());
        lqw.eq(StringUtils.isNotBlank(bo.getTimeConsuming()), StExperimentDatasView::getTimeConsuming, bo.getTimeConsuming());
        lqw.eq(StringUtils.isNotBlank(bo.getWearproofColorFastnessDry()), StExperimentDatasView::getWearproofColorFastnessDry, bo.getWearproofColorFastnessDry());
        lqw.eq(StringUtils.isNotBlank(bo.getWearproofColorFastnessWet()), StExperimentDatasView::getWearproofColorFastnessWet, bo.getWearproofColorFastnessWet());
        lqw.eq(StringUtils.isNotBlank(bo.getWashableColorFastnessStaining()), StExperimentDatasView::getWashableColorFastnessStaining, bo.getWashableColorFastnessStaining());
        lqw.eq(StringUtils.isNotBlank(bo.getWashableColorFastnessDiscolor()), StExperimentDatasView::getWashableColorFastnessDiscolor, bo.getWashableColorFastnessDiscolor());
        lqw.eq(StringUtils.isNotBlank(bo.getShade()), StExperimentDatasView::getShade, bo.getShade());
        lqw.eq(StringUtils.isNotBlank(bo.getBrilliance()), StExperimentDatasView::getBrilliance, bo.getBrilliance());
        lqw.eq(StringUtils.isNotBlank(bo.getWhetherDyedEvenly()), StExperimentDatasView::getWhetherDyedEvenly, bo.getWhetherDyedEvenly());
        lqw.eq(StringUtils.isNotBlank(bo.getAcceptable()), StExperimentDatasView::getAcceptable, bo.getAcceptable());
        return lqw;
    }

    /**
     * 新增全部实验数据
     */
    @Override
    public Boolean insertByBo(StExperimentDatasViewBo bo) {
        StExperimentDatasView add = BeanUtil.toBean(bo, StExperimentDatasView.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改全部实验数据
     */
    @Override
    public Boolean updateByBo(StExperimentDatasViewBo bo) {
        StExperimentDatasView update = BeanUtil.toBean(bo, StExperimentDatasView.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StExperimentDatasView entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除全部实验数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public String selectColorSchemeNameCountById(String ColorSchemeName) {
        return baseMapper.selectColorSchemeNameCount(ColorSchemeName);
    }
    @Override
    public String selectRecieptNameCountById(String RecieptName) {
        return baseMapper.selectRecieptNameCount(RecieptName);
    }
}
